package weaver.interfaces.xfjt.action;

import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.xfjt.utils.XfjtUtil;
import weaver.jobs.xfjt.duss.utils.WorkflowCreateHandler;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-10-10
 * @Description: 1、通讯录修改发起流程，根据主表字段（多人力）触发多个子流程：
 * 把触发的子流程数据反写流程转数据的建模的明细表。
 * @ModifyLog:
 **/
public class WeaAdressBookAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String currentDate = DateUtil.getCurrentDate();
        RecordSet rs = new RecordSet();
        String workflowId = "120";
        String workflowName = "通讯录修改";
        String isNextFlow = "1";
        String dataid = XfjtUtil.getIdByrequestid(requestid);
        writeLog(">>>>>WeaAdressBookAction>>>>>requestid" + requestid + ",dataid=" + dataid);
        WorkflowCreateHandler handler = new WorkflowCreateHandler();
        try {
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            //流程编号
            String lcbh = Util.null2String(rs.getString("lcbh"));
            lcbh = "".equals(lcbh) ? "''" : lcbh;
            //发起人
            String fqr = Util.null2String(rs.getString("fqr"));
            //是否触发
            String sfcf = Util.null2String(rs.getString("sfcf"));
            rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?", mainid);
            while (rs.next()) {
                Map<String, String> requestMainMap = new HashMap<>();
                //名称浏览
                String bmmc = Util.null2String(rs.getString("bmmc"));
                if (!"".equals(bmmc)) {
                    requestMainMap.put("jtbmxg", bmmc);
                    requestMainMap.put("tbbm", bmmc);
                }

                String txlgly = Util.null2String(rs.getString("txlgly"));
                if (!"".equals(txlgly)) {
                    requestMainMap.put("tbr", txlgly);
                }

                String fb = Util.null2String(rs.getString("fb"));
                if (!"".equals(fb)) {
                    requestMainMap.put("tbgs", fb);
                }

                requestMainMap.put("fqr", fqr);
                requestMainMap.put("fqlcbh", lcbh);
                requestMainMap.put("sfcf", sfcf);
                writeLog(">>>>requestMainMap" + requestMainMap.toString());
                ResourceComInfo rci = new ResourceComInfo();
                String requestName = workflowName + "-" + rci.getLastname(txlgly) + "-" + currentDate;
                String newrequestid = handler.WorkflowCreateByRequestMap(txlgly, workflowId, requestName, isNextFlow, requestMainMap);
                String sql1 = "insert into uf_xgfqtz_dt1 (mainid,tbr,jtbmxg,xglc) values (?,?,?,?) ";
                rs.executeUpdate(sql1, dataid, txlgly, bmmc, newrequestid);
            }

            rs.executeQuery("select * from " + tablename + "_dt2 where mainid=?", mainid);
            while (rs.next()) {
                Map<String, String> requestMainMap = new HashMap<>();
                //名称浏览
                String fbmc = Util.null2String(rs.getString("fbmc"));
                if (!"".equals(fbmc)) {
                    requestMainMap.put("zgsxg", fbmc);
                    requestMainMap.put("tbgs", fbmc);
                }
                String txlgly = Util.null2String(rs.getString("txlgly"));
                if (!"".equals(txlgly)) {
                    requestMainMap.put("tbr", txlgly);
                }

                String bm = Util.null2String(rs.getString("bm"));
                if (!"".equals(bm)) {
                    requestMainMap.put("jtbmxg", bm);
                    requestMainMap.put("tbbm", bm);
                }
                requestMainMap.put("fqr", fqr);
                requestMainMap.put("fqlcbh", lcbh);
                requestMainMap.put("sfcf", sfcf);
                writeLog(">>>>requestMainMap" + requestMainMap.toString());
                ResourceComInfo rci = new ResourceComInfo();
                String requestName = workflowName + "-" + rci.getLastname(txlgly) + "-" + currentDate;
                String newrequestid = handler.WorkflowCreateByRequestMap(txlgly, workflowId, requestName, isNextFlow, requestMainMap);
                String sql1 = "insert into uf_xgfqtz_dt1 (mainid,tbr,zgsxg,xglc) values (?,?,?,?) ";
                rs.executeUpdate(sql1, dataid, txlgly, fbmc, newrequestid);
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口异常！");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}
